package servlets.invitation;

import hibernate.method.InvitationMethods;
import hibernate.model.Invitation;
import hibernate.util.HibernateUtil;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * Servlet implementation class InvitationDelete
 */
@WebServlet("/InvitationDelete")
public class InvitationDelete extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InvitationDelete() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		
		
		String invitation_id = request.getParameter("check");
		SessionFactory sessionFactory = HibernateUtil
				.getSessionAnnotationFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		InvitationMethods inv_m = new InvitationMethods();
		//RelationMethods rel_m = new RelationMethods();
		Invitation inv = inv_m.invitationFromId(sessionFactory, session, Integer.parseInt(invitation_id));
		inv_m.deleteInvitation(sessionFactory, session, inv);
		
		try {
			session.getTransaction().commit();
			out.println("<script type=\"text/javascript\">alert(\"Zaproszenie usuniete\"); window.location.href = \"social/index.jsp?page=friends&group=invitations\";</script>");

		} catch (Exception e) {
			out.println("<script type=\"text/javascript\">alert(\"Usuniecie zaproszenia nie powiodlo sie\"); window.location.href = \"social/index.jsp?page=friends&group=invitations\";</script>");

		}
	}

}
